
/*=========================================
* Author: Administrator
* DateTime:2017/6/21 18:17:11
* Description:$safeprojectname$
==========================================*/

using System;
using System.Collections.Generic;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using UnityEngine;
using static UnityEditor.Progress;
using UnityEditor.PackageManager;

namespace OrderSystem
{
    public class CookProxy : Proxy
    {
        public new const string NAME = "CookProxy";
        /// <summary>
        /// 厨师集合
        /// 存储系统中所有厨师信息，通过Data属性实现数据访问
        /// </summary>
        public IList<CookItem> Cooks
        {
            get { return (IList<CookItem>)base.Data; }
        }
        List<Order> orders = new List<Order>();

        /// <summary>
        /// 构造函数
        /// 初始化厨师数据集合并加载默认厨师信息
        /// </summary>
        public CookProxy() : base(NAME, new List<CookItem>())
        {
            AddCook(new CookItem(1, "强尼", 0));
            AddCook(new CookItem(2, "托尼"));
            AddCook(new CookItem(3, "鲍比", 0));
            AddCook(new CookItem(4, "缇米"));
        }
        /// <summary>
        /// 添加新厨师到系统
        /// </summary>
        /// <param name="item">要添加的厨师实例</param>
        public void AddCook(CookItem item)
        {
            Cooks.Add(item);
        }
        public void RemoveCook(CookItem item)
        {
            Cooks.Remove(item);
        }

        /// <summary>
        /// 分配订单给空闲厨师
        /// </summary>
        /// <param name="order">要分配的订单实例</param>
        public void AssignOrderToCook(Order order)
        {
            for (int i = 0; i < Cooks.Count; i++)
            {
                if (Cooks[i].state == 0)//找到非忙碌厨师改变其状态
                {
                    Cooks[i].state = 1;
                    Cooks[i].cooking = order.names;//厨师抄的什么菜
                    Cooks[i].cookOrder = order;// 厨师炒菜的菜单
                    SendNotification(OrderSystemEvent.ResfrshCook);//找到空闲厨师去刷新一下厨师显示的状态
                    return;
                }
            }
            orders.Add(order); //如果没有空闲厨师，将订单添加到等待列表
            UnityEngine.Debug.Log("没有空闲厨师，订单已添加到等待列表: " + order.names);
        }

        internal void RemoveToCook(CookItem cookItem)
        {
            for (int i = 0; i < Cooks.Count; i++)
            {
                if (Cooks[i].id == cookItem.id)
                {
                    Cooks[i].state = 0;

                    SendNotification(OrderSystemEvent.SERVER_FOOD, cookItem);

                    if (orders.Count > 0)
                    {
                        Debug.Log("分配下一个订单给厨师: " + orders[0].names);

                        Cooks[i].state = 1; // 将厨师状态设置为忙碌

                        Cooks[i].cookOrder = orders[0]; // 分配下一个订单给厨师

                        orders.RemoveAt(0); // 分配完订单后从等待列表中移除

                        SendNotification(OrderSystemEvent.ResfrshCook);//找到空闲厨师去刷新一下厨师显示的状态
                    }

                    return;
                }
            }
        }
    }
}